package com.example.easeimpro.controller.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.EditText;

import com.example.easeimpro.R;
import com.example.easeimpro.model.bean.Model;
import com.example.easeimpro.model.bean.UserInfo;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;

public class RegisterActivity extends BaseActivity {
    private EditText registerUser;
    private static final String TAG = "RegisterActivity";
    private EditText registerPwd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        registerPwd = findViewById(R.id.registerPwd);
        registerUser = findViewById(R.id.registerUser);
        TranslateAnimation animation = new TranslateAnimation(0, 0, 800, 0);
        animation.setDuration(400);

        findViewById(R.id.loginItem).startAnimation(animation);
        findViewById(R.id.btn_register_apply).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = registerUser.getText().toString().trim();
                String password = registerPwd.getText().toString().trim();
                if (TextUtils.isEmpty(username)){
                    showToast("请输入用户名");
                }
                if (TextUtils.isEmpty(password)){
                    showToast("请输入密码");
                }
                Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            EMClient.getInstance().createAccount(username,password);
                            showToast("注册成功");
                            Model.getInstance().getAccountDbDao(mContext).addAccount(new UserInfo(username));
                        } catch (HyphenateException e) {
                            if(e.getMessage().equals("User already exist")){
                              showToast("用户已存在");
                            }
                            e.printStackTrace();
                        }
                    }
                });



            }
        });
    }
}